<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁严选商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="24px" />
          用户名:{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" width="20" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModel(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import {WeiXinLoginAPI} from "@/request/api";
import { mapActions, mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
     
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.LoginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal:state=>state.userInfo.cartTotal
      
    }),
  },
  methods: {
    ...mapMutations({
      //方法名:模块名/方法名
      changeIsShowLoginModel: "LoginModel/changeIsShowLoginModel",
      changeIsLogined: "LoginStatus/changeIsLogined",
      initUserInfo:"userInfo/initUserInfo"
    }),
    ...mapActions({
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo",
      
    })
  },
  async created() {
//获取code值 (当微信用户点击允许后,页面会重新刷新,也就会重新执行topbar中create钩子函数)
    let code = this.$route.query.code;
    if (code) {
      let res = await WeiXinLoginAPI({
        code,
      });
      console.log("微信登录后", res);

      if (res.code == 0) {
        //1.code==0 登录成功
        //1.1提示
        alert("扫码登录成功");
        //1.2存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //1.3更新右上角购物车按钮显示
        this.changeIsLogined(true);
        //1.4清除地址栏上的code
        this.$route.query.code && this.$router.push(this.$route.path);

        this.asyncChangeUserInfo()

      } else if (res.code == 400) {
        //2.code==400 二维码失效(code失败)
        //2.1提示重新扫码

        alert("二维码失败,请重新扫码");
        //2.2自动打开登录模块框
        this.changeIsShowLoginModel(true);
      } else if (res.code == 407) {
        //3.code==407 需要用户再进行一次手机号方式登录(后期让后台将微信和手机号码关联)
        //3.1提示用户需要进行一次手机号登陆
        alert("请使用手机号码再进行登录");
        //3.2自动打开登录模块框
        this.changeIsShowLoginModel(true);
        //3.3存储uuid (一会登录要用)
        localStorage.setItem("uuid", res.uuid);
      }
    }else{
      let token=localStorage.getItem("x-auth-token");
      this.changeIsLogined(Boolean(token))
      if(token){
        this.asyncChangeUserInfo()//更新用户信息
      }else{
        this.initUserInfo()//用户信息初始化.vuex(刷新页面和切换页面)
      }
    }
  },
  // watch: {
  //   //监听路由地址的变化
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         //判断当前是否还有token
  //         let token = localStorage.getItem("x-auth-token");
  //         this.changeIsLogined(Boolean(token));
  //       }
  //     },
  //   },
  // },
};
</script>
 
<style lang = "less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background-color: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>